package com.fakeofarty;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class FtripActivity extends Activity {
	/** Called when the activity is first created. */

	// LocationService mService;
	FtripService mService;
	boolean mBound = false;

	TextView mTextDistance;
	TextView mTextTotalDistance;
	TextView mTextSpeed;
	TextView mTextTime;
	TextView mTextPointNumber;
	TextView mTextGPSStatus;
	TextView mTextTrackTime;
	TextView mTextBattery;

	LinearLayout mLayoutCheckPoint;
	LinearLayout mLayoutBack;

	int mPaddingTop;
	int mPaddingBottom;
	int mPaddingLeft;
	int mPaddingRight;
	int mMarginTop;
	int mMarginBottom;
	int mMarginLeft;
	int mMarginRight;

	Handler mHandler = new Handler();

	void changeBackground(int resource) {
		mLayoutCheckPoint.setBackgroundResource(resource);
		mLayoutBack.setBackgroundResource(resource);
		setSmallSize(mLayoutCheckPoint);
		setSmallSize(mLayoutBack);
	}

	void start() {
		changeBackground(R.drawable.backgroundgreen);
		mService.start();
	}

	void stop() {
		changeBackground(R.drawable.background);
		mService.stop();
	}

	void chekPoint() {
		if (mService.putPoint()) {
			changeBackground(R.drawable.backgroundgreen);
		}
	}

	void goBack() {
		if (mService.back()) {
			changeBackground(R.drawable.backgroundred);
		} else {
			changeBackground(R.drawable.backgroundgreen);
		}
	}

	void setBigSize(LinearLayout layout) {
		layout.setPadding(mPaddingLeft + mMarginLeft, mPaddingTop + mMarginTop,
				mPaddingRight + mMarginRight, mPaddingBottom + mMarginBottom);
		((MarginLayoutParams) layout.getLayoutParams()).setMargins(0, 0, 0, 0);
	}

	void setSmallSize(LinearLayout layout) {
		layout.setPadding(mPaddingLeft, mPaddingTop, mPaddingRight,
				mPaddingBottom);
		((MarginLayoutParams) layout.getLayoutParams()).setMargins(mMarginLeft,
				mMarginTop, mMarginRight, mMarginBottom);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mLayoutCheckPoint = ((LinearLayout) findViewById(R.id.layoutCheckPoint));
		mLayoutBack = ((LinearLayout) findViewById(R.id.layoutBack));

		mPaddingTop = mLayoutCheckPoint.getPaddingTop();
		mPaddingBottom = mLayoutCheckPoint.getPaddingBottom();
		mPaddingLeft = mLayoutCheckPoint.getPaddingLeft();
		mPaddingRight = mLayoutCheckPoint.getPaddingRight();
		mMarginTop = ((MarginLayoutParams) mLayoutBack.getLayoutParams()).topMargin;
		mMarginBottom = ((MarginLayoutParams) mLayoutBack.getLayoutParams()).bottomMargin;
		mMarginLeft = ((MarginLayoutParams) mLayoutBack.getLayoutParams()).leftMargin;
		mMarginRight = ((MarginLayoutParams) mLayoutBack.getLayoutParams()).rightMargin;

		mLayoutCheckPoint.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					Log.d("fakeofarty", "point big");
					setBigSize(mLayoutCheckPoint);
					break;
				case MotionEvent.ACTION_UP:
					Log.d("fakeofarty", "point small");
					setSmallSize(mLayoutCheckPoint);
					break;
				}
				return false;
			}

		});

		mLayoutBack.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					Log.d("fakeofarty", "back big");
					setBigSize(mLayoutBack);
					break;
				case MotionEvent.ACTION_UP:
					Log.d("fakeofarty", "back small");
					setSmallSize(mLayoutBack);
					break;
				}
				return false;
			}
		});

		mLayoutCheckPoint.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				start();
				return true;
			}
		});

		mLayoutCheckPoint.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				chekPoint();
			}
		});

		mLayoutBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				goBack();
			}
		});

		mLayoutBack.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				stop();
				return true;
			}
		});

		mTextDistance = (TextView) findViewById(R.id.textDistance);
		mTextTotalDistance = (TextView) findViewById(R.id.textTotalDistance);
		mTextSpeed = (TextView) findViewById(R.id.textSpeed);
		mTextTime = (TextView) findViewById(R.id.textTime);
		mTextPointNumber = (TextView) findViewById(R.id.textPoint);
		mTextGPSStatus = (TextView) findViewById(R.id.textGPSStatus);
		mTextTrackTime = (TextView) findViewById(R.id.textTrackTime);
		mTextBattery = (TextView) findViewById(R.id.textBattery);

		// Bind to LocalService
		Intent intent = new Intent(this, FtripService.class);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (mBound) {
			mHandler.postDelayed(mUpdateTimeTask, 100);
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		mHandler.removeCallbacks(mUpdateTimeTask);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mBound) {
			unbindService(mConnection);
			mBound = false;
		}
	};

	/** Defines callbacks for service binding, passed to bindService() */
	private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName className, IBinder service) {
			mService = ((FtripService.LocalBinder) service).getService();
			mBound = true;

			mHandler.removeCallbacks(mUpdateTimeTask);
			mHandler.postDelayed(mUpdateTimeTask, 100);
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			mBound = false;
		}
	};

	private Runnable mUpdateTimeTask = new Runnable() {
		public void run() {
			DistanceData data = mService.getData();

			mTextTime.setText(data.getTimeShort());
			mTextDistance.setText(data.getDistance());
			mTextTotalDistance.setText(data.getTotalDistance());
			mTextPointNumber.setText(data.getPointNumberShort());
			mTextSpeed.setText(data.getSpeedShort());
			mTextGPSStatus.setText(data.getGpsStatusShort());
			mTextTrackTime.setText(data.getTrackTime());
			mTextBattery.setText(data.getBatteryShort());
			mHandler.postDelayed(mUpdateTimeTask, 250);
			// mHandler.postAtTime(this,
			// start + (((minutes * 60) + seconds + 1) * 1000));
		}
	};

	private Toast toast;
	private long lastBackPressTime = 0;

	@Override
	public void onBackPressed() {
		if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
			toast = Toast.makeText(this, "Press back again to close this app",
					4000);
			toast.show();
			this.lastBackPressTime = System.currentTimeMillis();
		} else {
			if (toast != null) {
				toast.cancel();
			}
			super.onBackPressed();
		}
	}
}